package com.zer0.tga.input;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.HashMap;
import java.util.Map;

import com.zer0.tga.input.Input.SingleInput;

public class InputHandler implements KeyListener,MouseListener,MouseMotionListener {
	
	private Map<Integer, SingleInput> keymap = new HashMap<Integer, SingleInput>();

	private Input inputs;
	
	public InputHandler(Input inputs) {
		
		keymap.put(KeyEvent.VK_UP, inputs.up);
		keymap.put(KeyEvent.VK_DOWN, inputs.down);
		keymap.put(KeyEvent.VK_LEFT, inputs.left);
		keymap.put(KeyEvent.VK_RIGHT, inputs.right);

		/*keymap.put(KeyEvent.VK_NUMPAD8, keys.map_up);
		keymap.put(KeyEvent.VK_NUMPAD2, keys.map_down);
		keymap.put(KeyEvent.VK_NUMPAD4, keys.map_left);
		keymap.put(KeyEvent.VK_NUMPAD6, keys.map_right);
*/
		keymap.put(KeyEvent.VK_W, inputs.up);
		keymap.put(KeyEvent.VK_S, inputs.down);
		keymap.put(KeyEvent.VK_A, inputs.left);
		keymap.put(KeyEvent.VK_D, inputs.right);

		keymap.put(KeyEvent.VK_SPACE, inputs.enter);
		keymap.put(KeyEvent.VK_ENTER, inputs.enter);
		keymap.put(KeyEvent.VK_BACK_SPACE, inputs.back);
		
		keymap.put(MouseEvent.BUTTON1, inputs.fire);
		keymap.put(MouseEvent.BUTTON3, inputs.bomb);
		
		this.inputs = inputs;
		
/*		mappings.put(KeyEvent.VK_CONTROL, keys.fire);
		mappings.put(KeyEvent.VK_SHIFT, keys.fire);
        mappings.put(KeyEvent.VK_C, keys.fire);

        mappings.put(KeyEvent.VK_X, keys.build);
        mappings.put(KeyEvent.VK_Z, keys.use);
		mappings.put(KeyEvent.VK_R, keys.build);
		mappings.put(KeyEvent.VK_E, keys.use);*/
	}
	

	private void toggle(KeyEvent ke, boolean state) 
	{
		SingleInput key = keymap.get(ke.getKeyCode());
		if (key != null) {
			key.nextState = state;
		}
	}
	
	private void toggle(MouseEvent me, boolean state)
	{
		SingleInput mouseButton = keymap.get(me.getButton());
		if(mouseButton != null){
			mouseButton.nextState = state;
		}
		
	}

	public void keyPressed(KeyEvent ke) {
		toggle(ke, true);
	}

	public void keyReleased(KeyEvent ke) {
		toggle(ke, false);
	}

	public void mousePressed(MouseEvent me) {
	//	System.out.println("Mouse Pressed: " +  me.getButton()+ " to " + me.getX() + " " + me.getY());
		toggle(me, true);
	}

	public void mouseReleased(MouseEvent me) {
		toggle(me, false);
	}
	
	
	public void keyTyped(KeyEvent ke) {
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void mouseDragged(MouseEvent me) {
		// TODO Auto-generated method stub
		mouseMoved(me);
		mousePressed(me);
	}


	@Override
	public void mouseMoved(MouseEvent me) {
		// TODO Auto-generated method stub
	//	System.out.println("asdasd");
	
		inputs.mousePosition.x = me.getX();
		inputs.mousePosition.y = me.getY();
	//	System.out.println(inputs.mousePosition.x + " " + inputs.mousePosition.y);
		
		//System.out.println(me.getX() + " " + me.getY());
		/*if(me.getX() != null && me.getY() != null)
		{
		inputs.mousePosition.x = me.getX();
		inputs.mousePosition.x = me.getY();
		}*/
		
	}





}
